package com.zf.shuashua.net;

import android.content.Context;
import android.os.Build;
import android.webkit.WebSettings;

import com.zf.shuashua.ShuaApplication;

import java.io.IOException;

import okhttp3.Interceptor;
import okhttp3.Request;
import okhttp3.Response;

public class MyHeaderIntercepter implements Interceptor {

    private String sysAgent = System.getProperty("http.agent") == null ? "" : System.getProperty("http.agent");

    private Context mContext;

    public MyHeaderIntercepter(Context mContext) {
        this.mContext = mContext;
    }

    @Override
    public Response intercept(Chain chain) throws IOException {
        Request request = chain.request()
                .newBuilder()
                .removeHeader("User-Agent")//移除旧的
                .addHeader("User-Agent", Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1 ? WebSettings.getDefaultUserAgent(ShuaApplication.context) : sysAgent)
                .build();
        return chain.proceed(request);
    }
}

